package com.winderinfo.yikaotianxia.ui.coupons;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class MyCouponsActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.coup_couponsId)
    EditText coupCouponsId;
    @BindView(R.id.coup_coupsonnum)
    TextView coupCoupsonnum;
    @BindView(R.id.coup_recycle)
    RecyclerView coupRecycle;
    @BindView(R.id.coup_code)
    EditText coupCode;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.activit_coupon;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
        coupRecycle.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        //获取用户Id
        String userID = spUtils.getString("UserID", "");
        if (userID != null) {
            //请求优惠券
            /**
             * @id 用户Id
             * */
            map.clear();
            map.put("userId", 1);
            presenter.onPostRequest(Api.COUPONS, map, CouponsBean.class);
        } else {
            ToastUtil.showSuccess(this, "请先登录");
        }
    }

    @Override
    public void onSuccess(Object data) {
        //优惠券数据
        if (data instanceof CouponsBean) {
            List<CouponsBean.RowsBean> rows = ((CouponsBean) data).getRows();
            coupCoupsonnum.setText("共有" + rows.size() + "张优惠券");
            CouponAdapter couponAdapter = new CouponAdapter(this, rows);
            coupRecycle.setAdapter(couponAdapter);
        }
        //激活优惠券
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, "激活成功");
            }
            if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, ((RegisterBean) data).getMsg());
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @OnClick({R.id.coup_back, R.id.coup_activation})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.coup_back:
                finish();
                break;
            case R.id.coup_activation:
                //获取输入框内容
                String couponNo = coupCouponsId.getText().toString();
                String couponCode = coupCode.getText().toString();
                //判断是否为空
                if (TextUtils.isEmpty(couponNo) || TextUtils.isEmpty(couponCode)) {
                    ToastUtil.showSuccess(this, "不能为空");
                    return;
                }
                //请求激活优惠券接口
                /**
                 * @id 用户Id
                 * @couponsNo 优惠券编码
                 * @code 激活码
                 * */
                String userID = spUtils.getString("UserID", "");
                map.clear();
                map.put("userId", userID);
                map.put("couponsNo", couponNo);
                map.put("code", couponCode);
                presenter.onPostRequest(Api.ACTIVATIONCOUPONS, map, RegisterBean.class);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }


}
